package per.kongtouxu.service.Impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import per.kongtouxu.dao.MenuDao;
import per.kongtouxu.dao.RoleDao;
import per.kongtouxu.domain.pojo.ROLE;
import per.kongtouxu.service.RoleService;

import javax.management.relation.Role;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private MenuDao menuDao;
    @Override
    public List<ROLE> findAll() {
        return roleDao.findAll();
    }

    @Override
    public void addRoleMenu(ROLE role, Integer[] menuIdList) {
        //1.添加角色
        roleDao.add(role);
        //2.添加角色菜单
        for (Integer menuId : menuIdList) {
            roleDao.addRoleMenu(role.getRoleId(),menuId);
        }
    }

    @Override
    public void delete(Integer RoleId) {
        //1.删除角色菜单
        roleDao.deleteRoleMenu(RoleId);
        //2.删除角色
        roleDao.delete(RoleId);
    }

    @Override
    public void update(ROLE role, Integer[] menuIdList) {
        roleDao.deleteRoleMenu(role.getRoleId());
        for (Integer menuId : menuIdList) {
            roleDao.addRoleMenu(role.getRoleId(),menuId);
        }
        roleDao.update(role);
    }
}
